//
//  SellRecordSelectedView.m
//  EasyToTesco2.0
//
//  Created by knew on 16/11/15.
//  Copyright © 2016年 EasyToTesco. All rights reserved.
//

#import "SellRecordSelectedView.h"

@interface SellRecordSelectedView ()

@property (nonatomic, strong)UIColor *unselectedColor;
@property (nonatomic, strong)UIColor *selectedColor;
@property (nonatomic,strong)NSArray *textArray;
@property (nonatomic,strong)NSMutableArray *buttonArray;

@end

@implementation SellRecordSelectedView

- (instancetype)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
        self.backgroundColor = BACKGROUND_COLOR;
        self.selectedColor = DEFAULT_COLOR;
        self.unselectedColor = [UIColor colorWithRed:181/255. green:180/255. blue:180/255. alpha:1];
        [self addSellRecordUI];
    }
    return self;
}

- (void)addSellRecordUI {
    CGFloat weight = CGRectGetWidth(self.frame)/self.textArray.count;
    CGFloat height = CGRectGetHeight(self.frame);
    for (NSInteger i = 0; i < self.textArray.count; i++) {
        UIButton *titleButton = [UIButton buttonWithType:UIButtonTypeCustom];
        titleButton.frame = CGRectMake(weight * i, 0, weight, height);
        titleButton.titleLabel.textAlignment = NSTextAlignmentCenter;
        titleButton.tag = 1000+i;
        titleButton.backgroundColor = [UIColor whiteColor];
        titleButton.titleLabel.font = [UIFont systemFontOfSize:17];
        titleButton.titleLabel.font = [UIFont fontWithName:@ "Arial Rounded MT Bold"  size:(18.0)];
        titleButton.layer.borderWidth = 1;
        titleButton.layer.borderColor = BACKGROUND_COLOR.CGColor;
        [titleButton setTitle:self.textArray[i] forState:UIControlStateNormal];
        [titleButton addTarget:self action:@selector(selectSellRecodeButtonClick:) forControlEvents:UIControlEventTouchUpInside];
        [titleButton setTitleColor:self.unselectedColor forState:UIControlStateNormal];
//        if (i==0) {
//            [titleButton setTitleColor:self.selectedColor forState:UIControlStateNormal];
//        }
        [self.buttonArray addObject:titleButton];
        [self addSubview:titleButton];
    }
}

- (void)selectSellRecodeButtonClick:(UIButton *)button {
    NSLog(@"%s",__func__);
    
    for (UIButton *tempBtn in self.buttonArray) {
        
        if (tempBtn.tag == button.tag) {
            [tempBtn setTitleColor:self.selectedColor forState:UIControlStateNormal];
            //            self.lineview.backgroundColor = [UIColor blackColor];
        }else{
            [tempBtn setTitleColor:self.unselectedColor forState:UIControlStateNormal];
            
            //            self.lineview.backgroundColor = [UIColor blackColor];
        }
        
        
    }
    
    if ([self.delegate respondsToSelector:@selector(didClickSelectedSellRecodeButton:)]) {
        [self.delegate didClickSelectedSellRecodeButton:button];
    }
    
}

- (NSArray *)textArray {
    if (!_textArray) {
        _textArray = @[@"已发货",@"未发货"];
    }
    return _textArray;
}

- (NSMutableArray *)buttonArray {
    if (!_buttonArray) {
        _buttonArray = [NSMutableArray array];
    }
    return _buttonArray;
}

@end
